﻿#include <conio.h>
#include <stdio.h>
#include <stdlib.h>

#include <Windows.h>

//添加joystick操作api的支持库
#include <MMSystem.h>
#pragma comment(lib, "Winmm.lib")

int main(int argc, char* argv[]) {
  //JOYINFO joyinfo;  //定义joystick信息结构体
  JOYINFOEX joyinfoex;
  joyinfoex.dwSize = sizeof(JOYINFOEX);
  joyinfoex.dwFlags = JOY_RETURNALL;
  while (1) {
    //读取手柄信息
    UINT joyNums;
    joyNums = joyGetNumDevs();
    //printf("当前手柄数量:%d \n",joyNums);
    if (joyNums >= 1) {
      MMRESULT joyreturn = joyGetPosEx(JOYSTICKID1, &joyinfoex);
      if (joyreturn == JOYERR_NOERROR) {
        printf("X:%5d,Y:%5d,Z:%5d,R:%5d,U:%5d,V:%5d,POV:%5d,Size:%5d,Flags:%5d,Buttton:%5d,ButtonN:%5d\n", 
          joyinfoex.dwXpos, 
          joyinfoex.dwYpos,
          joyinfoex.dwZpos,
          joyinfoex.dwRpos,
          joyinfoex.dwUpos,
          joyinfoex.dwVpos,
          joyinfoex.dwPOV,
          joyinfoex.dwSize,
          joyinfoex.dwFlags,
          joyinfoex.dwButtons,
          joyinfoex.dwButtonNumber);
        // printf("0x%09d ", joyinfoex.dwXpos);
        // printf("0x%09d ", joyinfoex.dwYpos);
        // printf("0x%09X ", joyinfoex.dwZpos);
        // printf("0x%09X ", joyinfoex.dwPOV);
        // printf("0x%09X ", joyinfoex.dwButtons);
        // printf("\n");
      } else {
        switch (joyreturn) {
          case JOYERR_PARMS:
            printf("bad parameters\n");
            break;
          case JOYERR_NOCANDO:
            printf("request not completed\n");
            break;
          case JOYERR_UNPLUGGED:
            printf("joystick is unplugged\n");
            break;
          default:
            printf("未知错误\n");
            break;
        }
      }
    }

    if (_kbhit()) break;
    Sleep(300);
  }
  return 0;
}